
#include "exynos_4412.h"



void uart2_init(void)
{

	/*UART2 initialize*/
	GPA1.CON = (GPA1.CON & ~0xFF ) | (0x22); //GPA1_0:RX;GPA1_1:TX

	UART2.ULCON2 = 0x3; //Normal mode, No parity,One stop bit,8 data bits
	UART2.UCON2 = 0x5;  //Interrupt request or polling mode

	/*
	 * Baud-rate 115200: src_clock:100Mhz
	 * DIV_VAL = (100*10^6 / (115200*16) -1) = (54.3 - 1) = 53.3
	 * UBRDIV2 = (Integer part of 53.3) = 53 = 0x35
	 * UFRACVAL2 = 0.3*16 = 0x5
	 * */
	UART2.UBRDIV2 = 0x35;
	UART2.UFRACVAL2 = 0x5;
}

void putc(const char data)
{
	while(!(UART2.UTRSTAT2 & 0X2));
	UART2.UTXH2 = data;
}

void puts(const  char  *pstr)
{
	while(*pstr != '\0')
		putc(*pstr++);
}

unsigned char getchar()
{
	unsigned char c;
	while(!(UART2.UTRSTAT2 & 0X1));
	c = UART2.URXH2;

	return c;
}

void gets(char *p)
{
	char data;
	while((data = getchar())!= '\n')   //在接收'\n'之前，已经接收了'\r'
		*p++ = data;
	if(data == '\n')
		*p++ = '\n';
	*p = '\0';
}

/*或者
void gets(char *p)
{
	char data;
	while(1)
	{
		*p++ = getchar();
		if (*(p-1) == '\n')             //在接收'\n'之前，已经接收了'\r'
			break;
	}
	*p = '\0';
}
*/

int strcmp(const char *src, const char *des)
{
	while(*src || *des)
	{
		if(*src > *des)
			return 1;
		else if(*src < *des)
			return -1;
		else
		{
			src++;
			des++;
		}
	}
	return 0;
}



void mydelayms(int ms)
{
    int i, j;
    while(ms--)
    {
        for (i = 0; i < 5; i++)
            for (j = 0; j < 514; j++);
    }
}
void key_flag(int* flag)/*判断GPX1_1引脚的状态，即判断按键是否按下*/
{
	if(!(GPX1.DAT & (0x1 << 1)))
		{
			mydelayms(10);
			if(!(GPX1.DAT & (0x1<<1)))    //二次检测，去抖
			{
				(*flag)++;
			}			
			while(!(GPX1.DAT & (1 << 1)));
		}
}

void mydelay_ms(int ms,int* flag)
{
    int i, j;
    while(ms--)
    {
        for (i = 0; i < 5; i++)
            for (j = 0; j < 514; j++);
			key_flag(flag);
    }
}

void LED_ALL_ON()
{
	GPX2.DAT |= 0x1 << 7;    //Turn on LED2
	GPX1.DAT |= 0x1;
	GPF3.DAT |= (0x1 << 4);
	GPF3.DAT |= (0x1 << 5);
}
void LED_ALL_OFF()
{
	GPX2.DAT &= ~(0x1 << 7);    //Turn off LED2			
	GPX1.DAT &= ~0x1;//Turn off LED3
	GPF3.DAT &= ~(0x1 << 4);  //Turn off LED4
	GPF3.DAT &= ~(0x1 << 5);	//Turn off LED5
}
void LED_Water(int *flag)
{
	
		//Turn on LED2
        GPX2.DAT |= 0x1 << 7;
        mydelay_ms(500,flag);

        //Turn on LED3
        GPX1.DAT |= 0x1;
        //Turn off LED2
        GPX2.DAT &= ~(0x1<<7);
        mydelay_ms(500,flag);


        //Turn on LED4
        GPF3.DAT |= (0x1 << 4);
        //Turn off LED3
        GPX1.DAT &= ~0x1;
        mydelay_ms(500,flag);


        //Turn on LED5
        GPF3.DAT |= (0x1 << 5);
        //Turn off LED4
        GPF3.DAT &= ~(0x1 << 4);       
        mydelay_ms(500,flag);

		//Turn off LED5
        GPF3.DAT &= ~(0x1 << 5);

}
int main()
{
    int flag = 0;
	char ch[20];
	
	uart2_init();

	GPX1.CON = GPX1.CON & (~(0xF << 4)); /*将GPX1_1设置成输入功能*/
	
	GPX2.CON = (GPX2.CON & ~(0xf<<28))| 1<<28;//GPX2_7:output, LED2
	GPX1.CON = (GPX1.CON & ~(0xf)) | 1; //GPX1_0:output, LED3
	GPF3.CON = (GPX3.CON & ~(0xf<<16 | 0xf<<20)) | (1<<16 | 1<<20);//GPF3_4:output, LED4 //GPF3_5:output, LED5
    LED_ALL_OFF();
	while(1)
	{

	    char *E = "mod1 key  mod2 uart";
		puts(E);
		gets(ch);
		while(!strcmp(ch, "mod1\r\n"))
		{
			gets(ch);
			key_flag(&flag);
			puts(flag+'0');

			if(flag == 1)
			{
				LED_ALL_ON();
				puts("MOD1:LED_ALL_ON");
			}
			else if(flag == 2)
			{
				LED_ALL_OFF();
				LED_Water(&flag);
				puts("MOD1:LED_Water");

			}
			else if(flag == 3)
			{
				flag = 0;
				LED_ALL_OFF();
				puts("MOD1:LED_ALL_OFF");
			}
		

		}
		
		while(1)
		{
			char *q = "1# LED_ALL_ON 2# LED_ALL_OFF 3#LED_WATER";
			puts(q);
			gets(ch);
			
			if(!strcmp(ch, "1#\r\n"))
			{
				LED_ALL_ON();
				puts("MOD2:LED_ALL_ON");
			}
			else if(!strcmp(ch, "2#\r\n"))
			{
				LED_ALL_OFF();
				puts("MOD2:LED_ALL_OFF");

			}
			else if(!strcmp(ch, "3#\r\n"))
			{
				LED_ALL_OFF();
				LED_Water(&flag);
				puts("MOD2:LED_Water");

			}	
			if(!strcmp(ch, "mod1\r\n"))
			{
				puts("MOD2 Exit, --->MOD1");
				break;
			}
		}
	
	 
		
	}
	return 0;
}
